package com.edu.base.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.edu.base.ums.studyRemind.domain.StudyRemind;
import com.edu.base.ums.studyRemind.service.IStudyRemindService;
import com.edu.utils.UserContextUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/study")
public class AppStudyController extends BaseController {

    @Autowired
    private IStudyRemindService studyRemindService;

    @GetMapping("/remind")
    public AjaxResult remindGet() {
        return AjaxResult.success(studyRemindService.getOne(new LambdaQueryWrapper<StudyRemind>()
                .select(StudyRemind::getWeeks, StudyRemind::getTime, StudyRemind::getRemark)
                .eq(StudyRemind::getMemberId, UserContextUtil.currentUser().getId())
        ));
    }

    @PostMapping("/remind")
    public AjaxResult remindSave(@RequestBody StudyRemind studyRemind) {
        StudyRemind one = studyRemindService.getOne(new LambdaQueryWrapper<StudyRemind>()
                .eq(StudyRemind::getMemberId, UserContextUtil.currentUser().getId())
        );

        if (one == null) {
            one = new StudyRemind();
            one.setMemberId(UserContextUtil.currentUser().getId());
            one.setCreateTime(new Date());
            one.setStatus(1);
            one.setDelFlag(0);
        }

        one.setWeeks(studyRemind.getWeeks());
        one.setTime(studyRemind.getTime());
        one.setRemark(studyRemind.getRemark());
        one.setUpdateTime(new Date());

        if (StringUtils.isNotBlank(one.getId())) {
            studyRemindService.updateById(one);
        } else {
            studyRemindService.save(one);
        }
        return AjaxResult.success();
    }

}
